package cn.tedu.adminsystem.webapi.security;

import cn.tedu.community.commons.excepion.ServiceException;
import cn.tedu.community.commons.restful.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.Serializable;

@Slf4j
public class CurrentAdmin implements Serializable {
    public static LoginPrincipal getAdminInfo(){
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken)
                SecurityContextHolder.getContext().getAuthentication();
        if (authenticationToken == null){
            throw new ServiceException(ServiceCode.ERR_UNAUTHORIZED,"请先登录！");
        }
        LoginPrincipal loginPrincipal = (LoginPrincipal) authenticationToken.getPrincipal();
        return loginPrincipal;
    }

    public static Long getAdminId(){
        return getAdminInfo().getId();
    }
}
